package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.servercenter.ConfigVersionCheckResultData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class ConfigVersionCheckResultDaoImpl extends TempDataObjectDao<ConfigVersionCheckResultData> {

    public static String getConfigVersionCheckResultDataSql = "select * from `t_s_config_version_check_result` where `serverId`=? and serverType=?";
    public ConfigVersionCheckResultData getConfigVersionCheckResultData(long serverId, int serverType) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getConfigVersionCheckResultDataSql);
            int i = 1;
            call.setLong(i++, serverId);
            call.setInt(i++, serverType);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }


    public static String updateConfigVersionCheckResultDataSql = "update `t_s_config_version_check_result` set channelId=?,configVersion=? where serverId=? and serverType=?";
    @Override
    protected boolean updateData(ConfigVersionCheckResultData configVersionCheckResultData) {
        configVersionCheckResultData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, configVersionCheckResultData.getChannelId()));
        para.put(i++, new DbParameter(Types.VARCHAR, configVersionCheckResultData.getConfigVersion()));
        para.put(i++, new DbParameter(Types.BIGINT, configVersionCheckResultData.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, configVersionCheckResultData.getServerType()));
        if (getDb().executeNoneQuery(updateConfigVersionCheckResultDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertConfigVersionCheckResultDataSql = "insert into `t_s_config_version_check_result`(serverId,serverType,channelId,configVersion) values (?,?,?,?)";
    @Override
    protected boolean addData(ConfigVersionCheckResultData configVersionCheckResultData) {
        configVersionCheckResultData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, configVersionCheckResultData.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, configVersionCheckResultData.getServerType()));
        para.put(i++, new DbParameter(Types.INTEGER, configVersionCheckResultData.getChannelId()));
        para.put(i++, new DbParameter(Types.VARCHAR, configVersionCheckResultData.getConfigVersion()));
        if (getDb().executeNoneQuery(insertConfigVersionCheckResultDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ConfigVersionCheckResultData parseData(ResultSet rs) throws SQLException {
        ConfigVersionCheckResultData configVersionCheckResultData = new ConfigVersionCheckResultData();
        configVersionCheckResultData.setServerId(rs.getLong("serverId"));
        configVersionCheckResultData.setServerType(rs.getInt("serverType"));
        configVersionCheckResultData.setChannelId(rs.getInt("channelId"));
        configVersionCheckResultData.setConfigVersion(rs.getString("configVersion"));

        return configVersionCheckResultData;
    }
}
